//
//  CatogryAllSpecialViewController.m
//  project
//
//  Created by lanou on 15/10/27.
//  Copyright © 2015年 lanou. All rights reserved.
//

#import "CatogryAllSpecialViewController.h"
#import "CatogryAllSpecialCell.h"
#import "PKRequestManager.h"
#import "CatogryAllSpecialModel.h"
#import "MJRefresh.h"
#import "CatogryAllSpecialDetailViewController.h"

@interface CatogryAllSpecialViewController () <UITableViewDataSource,UITableViewDelegate>
//创建一个tableView
@property(nonatomic,strong)UITableView *allSpecialTableView;
//创建一个接收数据的数组
@property(nonatomic,strong)NSMutableArray *dataSourceArray;

@end

@implementation CatogryAllSpecialViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //添加title
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake((kWidth - 40) / 2, 10, 40, 20)];
    titleLabel.text = @"全部专题";
    titleLabel.textColor = [UIColor whiteColor];
    self.navigationItem.titleView = titleLabel;
    
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    
    _allSpecialTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight) style:(UITableViewStylePlain)];
    _allSpecialTableView.backgroundColor = [UIColor whiteColor];
    _allSpecialTableView.delegate = self;
    _allSpecialTableView.dataSource = self;
    [_allSpecialTableView registerClass:[CatogryAllSpecialCell class] forCellReuseIdentifier:@"CatogryAllSpecialCell"];
    _allSpecialTableView.rowHeight = 150;
    _dataSourceArray = [NSMutableArray arrayWithCapacity:0];
    [self.view addSubview:_allSpecialTableView];
    
    //下拉刷新
    __weak CatogryAllSpecialViewController *newSelf = self;
//    self.allSpecialTableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//        [newSelf requestData];
//    }];
    
    //上拉加载
    self.allSpecialTableView.footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        [newSelf requestPullData];
    }];
    
    [self requestData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataSourceArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CatogryAllSpecialCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CatogryAllSpecialCell" forIndexPath:indexPath];
    cell.model = _dataSourceArray[indexPath.row];
    return cell;
}

static int offset = 0;
-(void)requestPullData
{
    offset = offset + 20;
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    
    [PKRequestManager requestWithTypeP:GET urlString:[NSString stringWithFormat: @"http://api.liwushuo.com/v2/collections?limit=20&offset=%d",offset] parDic:parDic finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
        NSDictionary *dict = dic[@"data"];
        NSArray *arr = dict[@"collections"];
        for (NSDictionary *dicModel in arr)
        {
            CatogryAllSpecialModel *model = [[CatogryAllSpecialModel alloc]init];
            [model setValuesForKeysWithDictionary:dicModel];
            [_dataSourceArray addObject:model];
        }
        [_allSpecialTableView reloadData];
        [_allSpecialTableView.footer endRefreshing];
        
    } error:^(NSError *error) {
        NSLog(@"数据请求失败");
    }];
}


-(void)requestData
{
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    
    [PKRequestManager requestWithTypeP:GET urlString:[NSString stringWithFormat: @"http://api.liwushuo.com/v2/collections?limit=20&offset=%d",offset] parDic:parDic finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
        NSDictionary *dict = dic[@"data"];
        NSArray *arr = dict[@"collections"];
        for (NSDictionary *dicModel in arr)
        {
            CatogryAllSpecialModel *model = [[CatogryAllSpecialModel alloc]init];
            [model setValuesForKeysWithDictionary:dicModel];
            [_dataSourceArray addObject:model];
        }
        [_allSpecialTableView reloadData];
    } error:^(NSError *error) {
        NSLog(@"数据请求失败");
    }];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    CatogryAllSpecialDetailViewController *specialDetailViewController = [[CatogryAllSpecialDetailViewController alloc]init];
    CatogryAllSpecialModel *model = _dataSourceArray[indexPath.row];
    specialDetailViewController.id = model.id;
    specialDetailViewController.titleLabel = model.title;
    [self.navigationController pushViewController:specialDetailViewController animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
